# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
@FileName:  test.py
@Time    :  2021-12-30 9:18
@Author  :  Alan_1999
@Version :  1.0
@License :  (C)Copyright 2021-2022
@Desc    :  
"""
import base64
import os

import pymysql

try:
    conn = pymysql.Connect(host='127.0.0.1',
                           port=3306,
                           user='root',
                           passwd='lales1999',
                           db='book_shop',
                           charset='utf8')
    print("数据库连接成功！")
except:
    print("请检查数据库连接。。。。。。。")
cur = conn.cursor()
imgs_li = os.listdir("./static/imgs")
temp = 1
for i in imgs_li:
    with open(f"./static/imgs/{i}", "rb+") as f:
        base64_data = base64.b64encode(f.read())
        s = base64_data.decode()
    try:
        # cur.execute("update book_info set book_img = '%s' where id = temp;" % (s))
        cur.execute("insert into test(img) values('%s');" % (s))
        print(f"{temp}插入成功！")
    except pymysql.ProgrammingError as e:
        print("插入出错！", e)
        break
    f.close()
    if temp > 200:
        break
    temp += 1
conn.commit()
conn.close()
cur.close()
